home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2792 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: newsroom.hitc.com!usenet
  2. From: psand@eos.hitc.com (G. Patrick Sand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Dynamically allocating memory for a char*
  5. Date: 19 Jan 1996 18:56:38 GMT
  6. Organization: Hughes Aircraft (EOSDIS)
  7. Message-ID: <4doph6$507@newsroom.hitc.com>
  8. References: <4dmn1i$10t@walrus2.walrus.com> <NITIN.96Jan19095012@more.eng.sun.com>
  9. NNTP-Posting-Host: 155.157.118.56
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.3
  12.  
  13. [SNIP]
  14. >In article <4dmn1i$10t@walrus2.walrus.com> fjordao@walrus.com (Felipe 
  15. Jordao) 
  16. >writes:
  17. >
  18. >> From: fjordao@walrus.com (Felipe Jordao)
  19. >> Newsgroups: comp.lang.c++
  20. >> Date: Fri, 19 Jan 1996 00:00:49 GMT
  21. >> Organization: HAC
  22. >> 
  23. >> Hi,
  24. >> 
  25. >> I have a question about allocating memory for a string as it's passed
  26. >> in by the user.  I'm using cin to get the info, but maybe this is
  27. >> inappropriate.  I have included the relevant snippets of the file
  28. >> below.  What I am trying to achieve is no limitation for the user when
  29. >> they input a path + filename.  Is it possible to use char* foo and
  30. >> then something like (I know this is wrong, but the idea of it ...)
  31. >> 
  32. >> cin >> malloc(sizeof(infile));             :0
  33. >> 
  34. >> Here is the actual code
  35. >> ----------------------------------------------------------------------
  36. >> 
  37. [SNIP,SNIP]
  38.  
  39. How about this:
  40.  
  41. use the cin.get( char* buf, int length, char term) method
  42.  
  43. This reads into a buffer (buf) up to length bytes and null-terminates it 
  44. (dropping the last byte if the stream read in has length non-null 
  45. characters), until it hits the term character.
  46.  
  47. Just pick a buffer size (like 512 or 1024) that you know they have no 
  48. right to exceed and you then manipulate the buffer to get your string...
  49.  
  50. I found this in section 10.3.1 (p. 332) of The C++ Programming Language 
  51. (2nd Edition) by Stroustrup (ISBN 0-201-53992-6)--mine is copyrighted 
  52. 1991
  53.  
  54. Of course, a later edition is probably out there; just look in the 
  55. Streams chapter on Output...
  56.  
  57. Hope this helps...
  58.  
  59. -- 
  60. G. Patrick Sand
  61. psand@eos.hitc.com
  62. PatSand@aol.com
  63. (301) 925-0791
  64. "Travel Light But Right..."
  65. Microsoft Network is prohibited from redistributing 
  66. this work in any form, in whole or in part.   License 
  67. to distribute this individual post is available to Microsoft
  68. for $999. Posting without permission constitutes an 
  69. agreement to these terms...gps
  70.  
  71.